﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineStore.Bll;
using OnlineStore.Dal;

namespace OnlineStore.Gui
{
    public partial class Cart : System.Web.UI.Page
    {
        int Page_Now = 0;
        List<bizCart> listCart;
        protected void Page_Load(object sender, EventArgs e)
        {
            listCart = (List<bizCart>)Session["cart"];
            if (IsPostBack)
            {
                bizCart _bizCart = new bizCart();
                if (listCart != null && mainCart != null)
                    for (int i = mainCart.Rows.Count-1;i >=0 ; i--)
                    {
                        string id = ((Label)mainCart.Rows[i].Cells[0].FindControl("id")).Text;
                        if (listCart[i] == id)
                        {
                            listCart[i].NumberOrder = int.Parse(((TextBox)mainCart.Rows[i].Cells[4].FindControl("Qty")).Text);
                            CheckBox chk = (CheckBox)mainCart.Rows[i].Cells[0].FindControl("Del");
                            if (chk.Checked)
                            {
                                Session["cart"] = _bizCart.DelCart((List<bizCart>)Session["cart"], id);
                            }
                        }
                    }
            }
            if (Page_Now == 0)
                Load_Data();
            if (mainCart != null)
            {
                double sum = 0;
                for (int i = 0; i < mainCart.Rows.Count; i++)
                {
                    sum += listCart[i].Sum;
                    if (listCart[i].NumberOrder > listCart[i].Stock)
                        ((Label)mainCart.Rows[i].Cells[2].FindControl("Warning")).Visible = true;
                }
                lb_Money.Text = String.Format("{0:0,0 VNĐ}", sum);
                lb_Sum_Money.Text = String.Format("{0:0,0 VNĐ}", sum);
            }
            if (Session["user"] != null)
            {
                HL_tab_login.NavigateUrl = "/gui/Account.aspx";
                HL_tab_login.Text = ((tbCustomer)Session["user"]).Username;
                HL_tab_Logout.Visible = true;
            }
            ListCartRight.DataSource = (List<bizCart>)Session["cart"];
            ListCartRight.DataBind();
            lb_Sum.Text = String.Format("{0:0,0 VNĐ}", bizCart.SumAllList((List<bizCart>)Session["cart"]));
            lb_Sum_product.Text = String.Format("{0:0,0 VNĐ}", bizCart.SumAllList((List<bizCart>)Session["cart"]));
        }
        protected void OnSubmit(object sender, EventArgs e)
        {

            string Address = this.tb_Address.Text;
            bizCart _bizCart = new bizCart();
            bizUser _bizUser = new bizUser();
            tbCustomer User = (tbCustomer) Session["user"];
            if (_bizCart.CheckOut(User, (List<bizCart>)Session["cart"]))
            {
                Session["cart"] = null;
                Response.Redirect("/gui/Account.aspx", true);
            }
            if(User==null)
                    Response.Redirect("/Gui/Login.aspx");
            lb_Warning.Visible = true;
            lb_Warning.Text = "Thanh toán thất bại";
            
        }

        protected void mainCart_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Page_Now = e.NewPageIndex;
            Load_Data();
        }
        private void Load_Data()
        {
            mainCart.PageIndex = Page_Now;
            mainCart.DataSource = listCart;
            mainCart.DataBind();
        }
    }
}